package com.jacky.webflux.controllers;

import com.jacky.webflux.commons.BasicResp;
import com.jacky.webflux.dtos.AppUserDto;
import com.jacky.webflux.exceptions.CommonException;
import com.jacky.webflux.services.AppUserService;
import jakarta.annotation.Resource;
import org.springframework.data.domain.*;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("appUser")
public class AppUserWrapController {

    @Resource
    private AppUserService appUserService;

    @GetMapping("/getWrapUser/{userId}")
    Mono<BasicResp<AppUserDto>> getAppUserWrap(@PathVariable("userId") Long userId) {
        return appUserService.getById(userId)
                .map(BasicResp::success)
                .defaultIfEmpty(BasicResp.fail("user not found"));
    }

    @PostMapping("/saveWrap")
    Mono<BasicResp<AppUserDto>> saveUser(@RequestBody AppUserDto dto) {
        return appUserService.saveUser(dto)
                .map(BasicResp::success)
                .defaultIfEmpty(BasicResp.fail("保存用户信息失败"));
    }

    @PutMapping("/updateWrap/{userId}")
    Mono<BasicResp<AppUserDto>> updateUser(@PathVariable("userId") Long userId, @RequestBody AppUserDto dto) {
        return appUserService.updateById(userId, dto)
                .map(BasicResp::success)
                .defaultIfEmpty(BasicResp.fail("更新用户信息失败"));
    }

    @GetMapping("/deleteWrap/{userId}")
    Mono<BasicResp<Void>> deleteAppUser(@PathVariable("userId") Long userId) {
        return appUserService.deleteById(userId)
                .map(BasicResp::success);
    }

    @GetMapping("/getAllWrap")
    Mono<BasicResp<List<AppUserDto>>> getAll() {
        return appUserService.allUserList()
                .map(BasicResp::success)
                .defaultIfEmpty(BasicResp.fail("no data"));
    }

    @GetMapping("/getWithPage")
    Mono<BasicResp<Page<AppUserDto>>> getWithPage(@Param("pageNum") Integer pageNum,
                                                  @Param("pageSize") Integer pageSize,
                                                  @Param("String") String sortStr) {
        PageRequest pageable = PageRequest.of(pageNum, pageSize)
                .withSort(Sort.Direction.fromString(sortStr.split(",")[1]), sortStr.split(",")[0]);
        return appUserService.getWithPage(pageable)
                .map(BasicResp::success)
                .defaultIfEmpty(BasicResp.success(new PageImpl<>(new ArrayList<>(), pageable, 0)));
    }

    /**
     * 测试全局异常处理
     * @param userId
     * @return
     */
    @GetMapping("/commonException/{userId}")
    Mono<BasicResp<AppUserDto>> commonException(@PathVariable("userId") Long userId) {
        if (userId <= 0) throw new CommonException(102, "cause commonException");
        return appUserService.getById(userId)
                .map(BasicResp::success)
                .defaultIfEmpty(BasicResp.fail("user not found"));
    }

}
